j <- readRDS("temp/tx_submitters.rds") |> 
  filter(!bad_counties2)

j <- j |> 
  mutate(submit = 0,
         accepted = ifelse(app_rejected_any, 2, 3),
         next_c = case_when(ballot_submitted & !ballot_rejected_any ~ 7,
                            !app_rejected_any & !ballot_submitted ~ 6,
                            ballot_rejected_any ~ 5,
                            app_rejected_any ~ 1),
         final = case_when(next_c == 7 ~ 7,
                           !app_rejected_any & !ballot_submitted & Primary_2022_03_01 == "Y" ~ 13,
                           !app_rejected_any & !ballot_submitted & Primary_2022_03_01 != "Y" ~ 12,
                           ballot_rejected_any & Primary_2022_03_01 == "Y" ~ 11,
                           ballot_rejected_any & Primary_2022_03_01 != "Y" ~ 10,
                           app_rejected_any & Primary_2022_03_01 == "Y" ~ 9,
                           app_rejected_any & Primary_2022_03_01 != "Y" ~ 8))



step1 <- j |> 
  group_by(app_rejected_any) |> 
  tally(name = "value") |> 
  mutate(source = 0,
         target = as.integer(!app_rejected_any) + 1,
         linkgr = ifelse(app_rejected_any, "red", "grey")) |> 
  select(-app_rejected_any)

app_rej <- j |> 
  filter(app_rejected_any) |> 
  mutate(source = 1,
         target = ifelse(Primary_2022_03_01 == "Y", 4, 3)) |> 
  group_by(source, target) |> 
  tally(name = "value") |> 
  mutate(linkgr = ifelse(target == 3, "red", "grey"))

step1 <- bind_rows(step1, app_rej)

app_acc <- j |> 
  filter(!app_rejected_any) |> 
  mutate(source = 2,
         target = case_when(ballot_rejected_any ~ 5,
                            !ballot_submitted ~ 6,
                            !ballot_rejected_any ~ 11)) |> 
  group_by(source, target) |> 
  tally(name = "value") |> 
  mutate(linkgr = ifelse(target == 5, "red", "grey"))

step1 <- bind_rows(step1, app_acc)

ball_rej <- j |> 
  filter(ballot_rejected_any) |> 
  mutate(source = 5,
         target = ifelse(Primary_2022_03_01 == "Y", 8, 7)) |> 
  group_by(source, target) |> 
  tally(name = "value") |> 
  mutate(linkgr = ifelse(target == 7, "red", "grey"))

step1 <- bind_rows(step1, ball_rej)


no_bal <- j |> 
  filter(!app_rejected_any, !ballot_submitted) |> 
  mutate(source = 6,
         target = ifelse(Primary_2022_03_01 == "Y", 10, 9)) |> 
  group_by(source, target) |> 
  tally(name = "value") |> 
  mutate(linkgr = "grey")

step1 <- bind_rows(step1, no_bal)


nodes <- data.frame(name = paste("Node", sort(unique(c(step1$source, step1$target))))) |> 
  mutate(ng = ifelse(name %in% c("Node 1", "Node 3",
                                 "Node 5", "Node 7"), "black", "black"),
         name = case_when(name == "Node 0" ~ paste0("Submitted Application: ", comma(sum(step1[step1$source == 0,]$value))),
                          name == "Node 2" ~ paste0("Application Accepted: ", comma(sum(step1[step1$source == 2,]$value))),
                          name == "Node 1" ~ paste0("Application Rejected: ", comma(sum(step1[step1$source == 1,]$value))),
                          name == "Node 5" ~ paste0("Ballot Rejected: ", comma(sum(step1[step1$source == 5,]$value))),
                          name == "Node 6" ~ paste0("Did Not Cast Mail Ballot: ", comma(sum(step1[step1$source == 6,]$value))),
                          name == "Node 3" ~ paste0("Did Not Vote: ", comma(sum(step1[step1$target == 3,]$value))),
                          name == "Node 4" ~ paste0("Voted in Person: ", comma(sum(step1[step1$target == 4,]$value))),
                          name == "Node 7" ~ paste0("Did Not Vote: ", comma(sum(step1[step1$target == 7,]$value))),
                          name == "Node 8" ~ paste0("Voted in Person: ", comma(sum(step1[step1$target == 8,]$value))),
                          name == "Node 9" ~ paste0("Did Not Vote: ", comma(sum(step1[step1$target == 9,]$value))),
                          name == "Node 10" ~ paste0("Voted in Person: ", comma(sum(step1[step1$target == 10,]$value))),
                          name == "Node 11" ~ paste0("Mail Ballot Accepted: ", comma(sum(step1[step1$target == 11,]$value))),
                          T ~ name))

node_color <- 'd3.scaleOrdinal() .range(["#cccccc", " #ff8080", " #000000 "])'

p <- sankeyNetwork(Links = step1, Nodes = nodes,
                   Source = "source", Target = "target",
                   Value = "value", NodeID = "name", LinkGroup = "linkgr",
                   colourScale = node_color, NodeGroup = "ng",
                   fontSize = 60, nodeWidth = 10, nodePadding = 40)

saveNetwork(p, "temp/sankey.html")
webshot::webshot("temp/sankey.html", paste0(out, "Figures/sankey.png"), vwidth = 3000, vheight = 1500)

